library(tidyverse) # for data cleaning and plotting
library(googlesheets4) # for reading googlesheet data
library(lubridate) # for date manipulation
library(openintro) # for the abbr2state() function
library(palmerpenguins)# for Palmer penguin data
library(maps) # for map data
library(ggmap) # for mapping points on maps
library(gplots) # for col2hex() function
library(RColorBrewer) # for color palettes
library(sf) # for working with spatial data
library(leaflet) # for highly customizable mapping
library(ggthemes) # for more themes (including theme_map())
library(plotly) # for the ggplotly() - basic interactivity
library(gganimate) # for adding animation layers to ggplots
library(transformr) # for "tweening" (gganimate)
library(shiny) # for creating interactive apps
library(babynames)
library(ggimage)
gs4_deauth() # To not have to authorize each time you knit.
theme_set(theme_minimal())
# SNCF Train data
small_trains <- read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-02-26/small_trains.csv")
# Lisa's garden data
garden_harvest <- read_sheet("https://docs.google.com/spreadsheets/d/1DekSazCzKqPS2jnGhKue7tLxRU3GVL1oxi-4bEM5IWw/edit?usp=sharing") %>%
mutate(date = ymd(date))
# Lisa's Mallorca cycling data
mallorca_bike_day7 <- read_csv("https://www.dropbox.com/s/zc6jan4ltmjtvy0/mallorca_bike_day7.csv?dl=1") %>%
select(1:4, speed)
# Heather Lendway's Ironman 70.3 Pan Am championships Panama data
panama_swim <- read_csv("https://raw.githubusercontent.com/llendway/gps-data/master/data/panama_swim_20160131.csv")
panama_bike <- read_csv("https://raw.githubusercontent.com/llendway/gps-data/master/data/panama_bike_20160131.csv")
panama_run <- read_csv("https://raw.githubusercontent.com/llendway/gps-data/master/data/panama_run_20160131.csv")
#COVID-19 data from the New York Times
covid19 <- read_csv("https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-states.csv")
Go here or to previous homework to remind yourself how to get set up.
Once your repository is created, you should always open your project rather than just opening an .Rmd file. You can do that by either clicking on the .Rproj file in your repository folder on your computer. Or, by going to the upper right hand corner in R Studio and clicking the arrow next to where it says Project: (None). You should see your project come up in that list if you’ve used it recently. You could also go to File –> Open Project and navigate to your .Rproj file.
Put your name at the top of the document.
For ALL graphs, you should include appropriate labels.
Feel free to change the default theme, which I currently have set to theme_minimal().
Use good coding practice. Read the short sections on good code with pipes and ggplot2. This is part of your grade!
NEW!! With animated graphs, add eval=FALSE to the code chunk that creates the animation and saves it using anim_save(). Add another code chunk to reread the gif back into the file. See the tutorial for help.
When you are finished with ALL the exercises, uncomment the options at the top so your document looks nicer. Don’t do it before then, or else you might miss some important warnings and messages.
ggplotly() function.beets_graph<-
garden_harvest%>%
filter(vegetable=="beets")%>%
group_by(variety, date)%>%
summarize(total_weight = sum(weight))%>%
mutate(weight_lbs = total_weight/454,
cum_weight = cumsum(weight_lbs))%>%
ggplot(aes(x=date, y=cum_weight, color=variety))+
geom_line()+
scale_color_manual(values=c("#e5cc13", "#46bf0a", "#9b035d"))+
labs(title="Total weight in harvests each day by beet variety", x = "Date", y= "Cumulative weight (lbs)")+
theme(legend.position = "top",
legend.title = element_blank())
ggplotly(beets_graph)
babynames_graph<-
babynames %>%
group_by(year, sex) %>%
top_n(n = 10, wt = n) %>%
summarize(top10prop = sum(prop)) %>%
ggplot() +
geom_line(aes(x=year, y=top10prop, color=sex))+
labs(title = "Proportion of names in the top 10 names by year", y= "proportion in top 10")
ggplotly(babynames_graph)
small_trains dataset that contains data from the SNCF (National Society of French Railways). These are Tidy Tuesday data! Read more about it here.trains_departure_anim<-
small_trains%>%
mutate(date = make_datetime(year, month))%>%
group_by(date, departure_station)%>%
mutate(prop_delayed_dep = sum(num_late_at_departure)/sum(total_num_trips))%>%
ggplot(aes(x=date, y=prop_delayed_dep))+
geom_jitter()+
labs(title = "Porportion of delayed departures",
subtitle = "Station: {closest_state}",
x = "",
y = "")+
transition_states(departure_station,
transition_length = 1,
state_length = 4) +
exit_shrink() +
enter_recolor(color = "lightblue") +
exit_recolor(color = "lightblue")
animate(trains_departure_anim, fps = 1)
anim_save("trains.gif", duration = 20)
knitr::include_graphics("trains.gif")
This plot shows the proportion of delayed departures over time for each station. It is interesting to see that most stations have an increased proportion of delayed departures over time, and that there are some stations with seasonal delays around the middle ore end of the year.
geom_area() examples here). You will look at cumulative harvest of tomato varieties over time. You should do the following:garden_harvest data, filter the data to the tomatoes and find the daily harvest in pounds for each variety.fct_reorder()) from most to least harvested (most on the bottom).I have started the code for you below. The complete() function creates a row for all unique date/variety combinations. If a variety is not harvested on one of the harvest dates in the dataset, it is filled with a value of 0.
garden_harvest %>%
filter(vegetable == "tomatoes") %>%
group_by(date, variety) %>%
summarize(daily_harvest_lb = sum(weight)*0.00220462) %>%
ungroup() %>%
complete(variety, date, fill = list(daily_harvest_lb = 0))%>%
group_by(variety)%>%
mutate(cum_weight = cumsum(daily_harvest_lb))%>%
ungroup() %>%
mutate(variety = fct_reorder(variety, daily_harvest_lb, sum, .desc = FALSE))%>%
ggplot(aes(x=date, y=cum_weight, fill = variety))+
geom_area(position = "stack")+
labs(title = "Cumulative harvest weight",
x = "",
y = "",
color = "variety")+
theme(legend.position = "right",
legend.title = element_blank())
garden_harvest %>%
filter(vegetable == "tomatoes") %>%
group_by(date, variety) %>%
summarize(daily_harvest_lb = sum(weight)*0.00220462) %>%
ungroup() %>%
complete(variety, date, fill = list(daily_harvest_lb = 0))%>%
group_by(variety)%>%
mutate(cum_weight = cumsum(daily_harvest_lb))%>%
ungroup() %>%
mutate(variety = fct_reorder(variety, daily_harvest_lb, sum, .desc = FALSE))%>%
ggplot(aes(x=date, y=cum_weight, fill = variety))+
geom_area(position = "stack")+
labs(title = "Cumulative harvest weight",
subtitle = "Date: {frame_along}",
x = "",
y = "",
color = "variety")+
theme(legend.position = "top",
legend.title = element_blank())+
transition_reveal(date)
anim_save("tomatoes.gif", duration = 20)
knitr::include_graphics("tomatoes.gif")
mallorca_bike_day7 bike ride using animation! Requirements:ggmap.ggimage package and geom_image to add a bike image instead of a red point. You can use this image. See here for an example.mallorca_map<-get_stamenmap(
bbox = c(left = 2.30, bottom = 39.53, right = 2.62, top = 39.72),
maptype = "terrain",
zoom = 11)
bike_image_link<-"https://raw.githubusercontent.com/llendway/animation_and_interactivity/master/bike.png"
mallorca_bike_new <- mallorca_bike_day7%>%
mutate(image = bike_image_link)
ggmap(mallorca_map) +
geom_path(data = mallorca_bike_new,
aes(x = lon, y = lat, color = ele))+
geom_image(data = mallorca_bike_new,
aes(image = image), size = .08)+
scale_color_gradient(low = "red", high = "blue")+
transition_reveal(time)+
theme_map()+
labs(title = "Mallorca Bike Trip",
subtitle = "Time: {frame_along}")
anim_save("biking.gif")
knitr::include_graphics("biking.gif")
This is definitely preferable to a static map. The route is more clear with an animated map, and we can see pauses or breaks in the ride that makes it feel like a story is being told.
panama_swim, panama_bike, and panama_run. Create a similar map to the one you created with my cycling data. You will need to make some small changes: 1. combine the files (HINT: bind_rows(), 2. make the leading dot a different color depending on the event (for an extra challenge, make it a different image using `geom_image()!), 3. CHALLENGE (optional): color by speed, which you will need to compute on your own from the data. You can read Heather’s race report here. She is also in the Macalester Athletics Hall of Fame and still has records at the pool.bike_run<-bind_rows(panama_bike, panama_run)
bike_run_swim<- bind_rows(bike_run, panama_swim)
panama_map<-get_stamenmap(
bbox = c(left = -79.60, bottom = 8.89, right = -79.44, top = 9.01),
maptype = "terrain",
zoom = 13)
ggmap(panama_map)+
theme_map()
ggmap(panama_map) +
geom_path(data = bike_run_swim,
aes(x = lon, y = lat, group = event))+
geom_point(data = bike_run_swim,
aes(x = lon, y = lat, color = event), size = 3)+
transition_reveal(time)+
theme_map()+
labs(title = "Ironman 70.3 Pan Am Championships",
subtitle = "Time: {frame_along}")
anim_save("ironman.gif")
knitr::include_graphics("ironman.gif")
lag() function you’ve used in a previous set of exercises). Replace missing values with 0’s using replace_na().geom_path() and add a group aesthetic. Put the x and y axis on the log scale and make the tick labels look nice - scales::comma is one option. This plot will look pretty ugly as is.geom_point()) and add the state name as a label (geom_text() - you should look at the check_overlap argument).animate() function to have 200 frames in your animation and make it 30 seconds long.covid19%>%
group_by(state)%>%
mutate(lag7 = lag(cases, 7, order_by = date ))%>%
replace_na(list(lag7 = 0)) %>%
mutate(avg_7_day=(cases - lag7)/7)%>%
filter(cases>=20)%>%
ggplot(aes(x=cases, y=lag7, group = state))+
geom_path()+
scale_y_log10(labels = scales::comma)+
scale_x_log10(labels = scales::comma)+
labs(title = "New Cases of COVID-19 by Total Number of Cases",
x = "New Cases",
y = "Total Cases")+
theme(legend.position = "none")
covid19_animation<-
covid19%>%
group_by(state)%>%
mutate(lag7 = lag(cases, 7, order_by = date ))%>%
replace_na(list(lag7 = 0)) %>%
mutate(avg_7_day=(cases - lag7)/7)%>%
filter(cases>=20)%>%
ggplot(aes(x=cases, y=lag7, group = state, color = state))+
geom_path()+
geom_point()+
geom_text(aes(label = state), check_overlap = TRUE)+
scale_color_viridis_d("viridis") +
scale_y_log10(labels = scales::comma)+
scale_x_log10(labels = scales::comma)+
transition_reveal(date)+
labs(title = "New Cases of COVID-19 by Total Number of Cases",
subtitle = "Date: {frame_along}",
x = "",
y = "",
color = "state") +
theme(legend.position = "none")
animate(covid19_animation, nframes = 200, duration = 30)
anim_save("covid19.gif")
knitr::include_graphics("covid19.gif")
States with large populations, like California and New York, really took off right at the start with new cases as a proportion of total cases. Most all states at the beginning kind of took off, by later dates it looks like it is slowing down because of the scale of the y-axis. Both of these graphs are messy and hard to interpret, but the animated one is a bit easier to understand.
wday() to create a day of week variable and filter to all the Fridays.census_pop_est_2018 <- read_csv("https://www.dropbox.com/s/6txwv3b4ng7pepe/us_census_2018_state_pop_est.csv?dl=1") %>%
separate(state, into = c("dot","state"), extra = "merge") %>%
select(-dot) %>%
mutate(state = str_to_lower(state))
states_map <- map_data("state")
covid19%>%
filter(!state %in% c("alaska","hawaii","guam","virgin islands", "puerto rico", "northern mariana islands"))%>%
mutate(state = str_to_lower(state))%>%
left_join(census_pop_est_2018)%>%
mutate(covid_10000 = (cases/est_pop_2018)*10000,
day_of_week = wday(date, label = TRUE))%>%
filter(day_of_week == "Fri")%>%
ggplot()+
geom_map(map = states_map,
aes(map_id = state,
fill = covid_10000,
group = date))+
expand_limits(x = states_map$long, y = states_map$lat)+
theme_map()+
labs(title = "Most Recent Cumulative COVID-19 Cases per 10,000 People",
subtitle = "Date: {closest_state}")+
transition_states(date)
anim_save("covid_states.gif")
knitr::include_graphics("covid_states.gif")
This map looks much better for visualizing COVID spread than the previous one. It is really cool to see the progressive spread of COVID with the animation.
DID YOU REMEMBER TO UNCOMMENT THE OPTIONS AT THE TOP?